package com.hospital.workstation.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.hospital.workstation.utils.CXFClientUtil;
import com.hospital.workstation.webService.domain.Response;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.hospital.common.annotation.RepeatSubmit;
import com.hospital.common.annotation.Log;
import com.hospital.common.core.controller.BaseController;
import com.hospital.common.core.domain.PageQuery;
import com.hospital.common.core.domain.R;
import com.hospital.common.core.validate.AddGroup;
import com.hospital.common.core.validate.EditGroup;
import com.hospital.common.core.validate.QueryGroup;
import com.hospital.common.enums.BusinessType;
import com.hospital.common.utils.poi.ExcelUtil;
import com.hospital.workstation.domain.vo.PatientBaseInfoVo;
import com.hospital.workstation.domain.bo.PatientBaseInfoBo;
import com.hospital.workstation.service.IPatientBaseInfoService;
import com.hospital.common.core.page.TableDataInfo;

/**
 * 患者基本信息
 *
 * @author whb
 * @date 2023-02-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workstation/baseInfo")
public class PatientBaseInfoController extends BaseController {

    private final IPatientBaseInfoService iPatientBaseInfoService;

    /**
     * 查询患者基本信息列表
     */
    @SaCheckPermission("workstation:baseInfo:list")
    @GetMapping("/list")
    public TableDataInfo<PatientBaseInfoVo> list(PatientBaseInfoBo bo, PageQuery pageQuery) {
        return iPatientBaseInfoService.queryPageListOfPatientInfo(bo, pageQuery);
    }

    /**
     * 获取患者基本信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("workstation:baseInfo:query")
    @GetMapping("/{id}")
    public R<PatientBaseInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long id) {
        return R.ok(iPatientBaseInfoService.queryById(id));
    }

    /**
     * 新增患者基本信息
     */
    @SaCheckPermission("workstation:baseInfo:add")
    @Log(title = "患者基本信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatientBaseInfoBo bo) {
        return toAjax(iPatientBaseInfoService.insertByBo(bo));
    }

    /**
     * 修改患者基本信息
     */
    @SaCheckPermission("workstation:baseInfo:edit")
    @Log(title = "患者基本信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatientBaseInfoBo bo) {
        return toAjax(iPatientBaseInfoService.updateByBo(bo));
    }

    /**
     * 删除患者基本信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("workstation:baseInfo:remove")
    @Log(title = "患者基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPatientBaseInfoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 出院
     *
     * @param id 患者ID
     * @return
     */
    @SaCheckPermission("workstation:baseInfo:leave")
    @Log(title = "出院", businessType = BusinessType.UPDATE)
    @GetMapping("/leave/{id}")
    public R<Void> leave(@PathVariable Long id) {
        return iPatientBaseInfoService.leave(id);
    }

    /**
     * 查询历史病历
     *
     * @param medicalRecordNo 病历号
     * @param pageQuery       分页对象
     * @return
     */
    @SaCheckPermission("workstation:baseInfo:history")
    @GetMapping("/history")
    public TableDataInfo<PatientBaseInfoVo> queryHistoryList(Integer medicalRecordNo, PageQuery pageQuery) {
        return iPatientBaseInfoService.queryHistoryPageList(medicalRecordNo, pageQuery);
    }

    /**
     * 查询患者监护分级
     *
     * @param medicalRecordNo 病历号
     * @return
     */
    @GetMapping("/guardianLevel/{medicalRecordNo}")
    public R<String> queryGuardianLevel(@PathVariable Integer medicalRecordNo) {
        return iPatientBaseInfoService.queryGuardianLevel(medicalRecordNo);
    }

    /**
     * 测试调用HIS病历信息接口
     *
     * @return
     */
    @SaCheckPermission("workstation:baseInfo:medical:info")
    @GetMapping("/medical/info")
    public R<String> getXml() {
        Map<String, Object> params = new HashMap<>();
        params.put("code", "MES0181");
        params.put("xml", new Response().getMES0181Xml("9174763"));
        String hipMessageInfo = CXFClientUtil.sendXml("HIPMessageInfo", params);
        return R.ok("调用成功", hipMessageInfo);
        /*params.put("code", "MES0073");
        params.put("xml", new Response().getMES0073Xml());
        String sendMessageInfo = CXFClientUtil.sendXml("SendMessageInfo", params);
        return R.ok("调用成功", sendMessageInfo);*/
    }

    /**
     * 获取不良反应参数拼接字符串
     *
     * @param userId 用户id
     * @return
     */
    @GetMapping("/adr/{userId}")
    public R<String> getAdrParams(@PathVariable Long userId) {
        return iPatientBaseInfoService.getAdrParams(userId);
    }
}
